package com.roubsite.cloud.common.utils;

import java.io.*;

public class BeanUtils {
	public static byte[] toBytes(Object mi) {
		try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(os)) {
			objectOutputStream.writeObject(mi);
			objectOutputStream.flush();
			os.flush();
			byte[] bytes = os.toByteArray();
			return bytes;
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 释放file
		return null;

	}

	@SuppressWarnings("unchecked")
	public static <T> T toBean(byte[] bytes, Class<T> bean) {
		T ret = null;
		try (InputStream is = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(is)) {
			ret = (T) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}

}
